<template>
  <transition name="messagebox-fade">
    <div v-show="visible" class="messageBox-box">
      <div class="lockscreen-item">
        <div class="loc-date">
          {{ nowTime }}
        </div>

        <div class="mt-4">
          <el-input
            v-model="state.lockValue"
            placeholder="请输入解锁密码"
            class="el_input"
          >
            <template #prepend
              ><el-button @click="toggleBg">切换壁纸 </el-button>
            </template>
            <template #append>
              <el-button :icon="Unlock" @click="cancalBtnClick" />
            </template>
          </el-input>
        </div>
        <!-- <div class="loc-input">
          <input type="text" v-model="state.lockValue" />
        </div>
        <div class="loc-btn">
          <el-button type="primary" @click="cancalBtnClick">解锁</el-button>
        </div> -->
      </div>
    </div>
  </transition>
</template>
<script setup>
import { reactive, toRefs, onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import { Unlock } from "@element-plus/icons-vue";
const state = reactive({
  visible: false,
  promptValue: "",
  type: "",
  lockValue: "",
});
const { visible } = toRefs(state);
const setVisible = (isVisible) => {
  state.visible = isVisible;
};
const cancalBtnClick = () => {
  let item = localStorage.getItem("lockScreen");
  if (item === state.lockValue) {
    localStorage.removeItem("lockScreen");
    setVisible(false);
  } else {
    return ElMessage.error("密码错误");
  }
};
defineExpose({
  setVisible,
  state,
});
//当前时间
const nowTime = ref("");
/**
 * 将小于10的数字前面补0
 * @function
 * @param {number} value
 * @returns {string} 返回补0后的字符串
 */
const complement = (value) => {
  return value < 10 ? `0${value}` : value.toString();
};
/**
 * 格式化时间为XXXX年XX月XX日XX时XX分XX秒
 * @function
 * @returns {string} 返回格式化后的时间
 */
const formateDate = () => {
  const time = new Date();
  const year = time.getFullYear();
  const month = complement(time.getMonth() + 1);
  const day = complement(time.getDate());
  const hour = complement(time.getHours());
  const minute = complement(time.getMinutes());
  const second = complement(time.getSeconds());
  const week = "星期" + "日一二三四五六".charAt(time.getDay());
  return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
};
const timer = ref(0);

//切换壁纸
const backgrounds = ["background1.jpg", "background2.jpg", "background3.jpg"];

onMounted(() => {
  /**
   * 设置定时器
   */
  timer.value = setInterval(() => {
    nowTime.value = formateDate();
  }, 1000);
});

/**
 * 取消定时器
 */
onBeforeUnmount(() => {
  clearInterval(timer); //清除定时器
  timer.value = 0;
});
</script>
<style lang="scss" scoped>
.messagebox-fade-enter-from,
.messagebox-fade-leave-to {
  opacity: 0;
  border: 1px solid red;
}
.messagebox-fade-enter-active {
  transition: opacity 0.2s ease-in;
}
.messagebox-fade-leave-active {
  transition: opacity 0.2s ease-out;
}
.messageBox-box {
  border: 1px solid #000;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  // background: #000;
  background: url("../assets/images/bg.png") no-repeat center center fixed;
  background-size: cover;

  // background-color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  animation: identifier 4s;
  @keyframes identifier {
    0% {
      // margin-left: 100vw;
      transform: scale(1);
    }
    50% {
      transform: scale(1.1); /* 放大比例 */
    }
    100% {
      // margin-top: 0;
      transform: scale(1);
    }
  }
  .lockscreen-item {
    color: #fff;
    text-align: center;
    align-content: center;
    justify-content: center;
    width: 100%;
    // border: 1px solid greenyellow;
  }

  .loc-date {
    text-align: center;
    font-size: 40px;
    margin-bottom: 100px;
  }
  .loc-week {
    text-align: center;
    font-size: 30px;
    margin-top: 10px;
  }
  .loc-time {
    font-size: 80px;
    font-weight: bold;
    margin-top: 10px;
  }
  :deep(.el_input) {
    height: 50px;
    width: 60%;
    border-radius: 0px;
    border: 1px solid #000;
  }
  .loc-input {
    margin-top: 10px;
    input {
      margin: 40px 0;
      box-sizing: border-box;
      font-variant: tabular-nums;
      list-style: none;
      font-feature-settings: tnum;
      position: relative;
      display: inline-block;
      width: 100%;
      padding: 4px 11px;
      color: #000000d9;
      font-size: 14px;
      line-height: 1.5715;
      background-color: #fff;
      background-image: none;
      border: 1px solid #d9d9d9;
      border-radius: 2px;
      transition: all 0.3s;
      outline: none;
    }
  }
}
</style>
